package com.serviceprovider.business.module.admin.transaction.services.dao.impl;

//~--- non-JDK imports --------------------------------------------------------

import com.serviceprovider.business.module.admin.transaction.model.NewCsvReplica;
import com.serviceprovider.business.module.admin.transaction.services.dao.interfaces.IFlatTableService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.simple.SimpleJdbcDaoSupport;
import org.springframework.stereotype.Repository;

import org.supercsv.io.ICsvMapReader;

//~--- JDK imports ------------------------------------------------------------

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Repository("flatTableService")
public class FlatTableServiceImpl extends SimpleJdbcDaoSupport implements IFlatTableService {
    @Autowired
    public FlatTableServiceImpl(@Qualifier("jdbcTemplate") JdbcTemplate jdbcTemplate) {
        setJdbcTemplate(jdbcTemplate);
    }

    @Override
    public int insertBatch(final ICsvMapReader reader, final String[] headers, String flatTableFields,
                           String[] flatTableFieldsOrder, int historyId, int mappingId)
            throws Exception {
        List<Object[]>      batch = new ArrayList<Object[]>();
        Map<String, String> row;
        int                 j      = 0;
        String[]            header = reader.getCSVHeader(true);

        while ((row = reader.read(header)) != null) {
            Object[] values = new Object[flatTableFieldsOrder.length + 1];    // Add 1 for historyid

            values[0] = historyId;
            values[1] = mappingId;

            for (int i = 0; i < flatTableFieldsOrder.length; i++) {
                if (i > headers.length) {
                    values[i + 2] = "";
                } else {
                    values[i + 2] = row.get(headers[i]);
                }
            }

            batch.add(values);
            j += 1;
        }

        String questionMarks = "?,";

        for (String aFlatTableFieldsOrder : flatTableFieldsOrder) {
            questionMarks += "?,";
        }

        questionMarks   += "?";
        flatTableFields = flatTableFields.substring(0, flatTableFields.length() - 1);

        int[] updateCounts = getSimpleJdbcTemplate().batchUpdate("insert into newCsvReplica (mapping_id,historyId,"
                                 + flatTableFields + ") values(" + questionMarks + ")", batch);

        return updateCounts.length;
    }

    @Override
    public ArrayList<NewCsvReplica> getAllRecords(int historyId) {
        Map                      row        = null;
        ArrayList<NewCsvReplica> recordList = new ArrayList<NewCsvReplica>();
        List                     list       = getJdbcTemplate().queryForList("select * from newCsvReplica where historyId=?",
                        new Object[] { historyId });

        for (Object obj : list) {
            row = (Map) obj;

            NewCsvReplica record = new NewCsvReplica();

            record.setId((Integer) row.get("id"));
            record.setHistoryId((Integer) row.get("historyId"));
            record.setReceiver_name((String) row.get("receiver_name"));
            record.setService_provider_id((String) row.get("service_provider_id"));
            record.setReceiver_name((String) row.get("receiver_name"));
            record.setOriginator_name((String) row.get("originator_name"));
            record.setTransaction_amount((String) row.get("transaction_amount"));
            record.setOrginator_code((String) row.get("originator_code"));
            record.setTrace_number((String) row.get("trace_number"));
            record.setExtra_filed1((String) row.get("extra_filed1"));
            record.setExtra_filed2((String) row.get("extra_filed2"));
            record.setExtra_filed3((String) row.get("extra_filed3"));
            record.setExtra_filed4((String) row.get("extra_filed4"));
            record.setExtra_filed5((String) row.get("extra_filed5"));
            recordList.add(record);
        }

        return recordList;
    }
}



